package com.prj.ufdm.cloud.ms.auth.bpo._impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.prj.ufdm.cloud.base.util.BaseUuidUtil;
import com.prj.ufdm.cloud.ms.auth.bpo.SysDutyOrganBPO;
import com.prj.ufdm.cloud.ms.auth.dao.SysDutyOrganDao;
import com.prj.ufdm.cloud.ms.auth.dao.SysOrganDao;
import com.prj.ufdm.cloud.ms.auth.model.SysDutyOrgan;
import com.prj.ufdm.cloud.ms.auth.model.SysOrgan;
import com.prj.ufdm.cloud.ms.core.bpo._impl.BaseBPOImpl;

/**
 * DESC: 职务机构功能 <br>
 * @author 胡义振
 * @date 2020年1月14日
 */
@Service 
public class SysDutyOrganBPOImpl extends BaseBPOImpl<SysOrganDao,SysOrgan> implements SysDutyOrganBPO{
	
    @Autowired
    private SysDutyOrganDao sysDutyOrganDao;
    
	/**
	 * DESC: 添加职务机构对应关系
	 * @auther 胡义振
	 * @date 2020年1月16日
	 * @param dutyId
	 * @param organIds
	 * @throws Exception 
	 * @return
	 */
	@Override
	public void doAddDutyOrgan(String dutyId, String organIds) throws Exception {
		sysDutyOrganDao.deleteDutyOrganByDutyId(dutyId);
		if(organIds!=null && !organIds.equals("")) {
			String[] arrOrganId = organIds.split(",");
			List<SysDutyOrgan> listSysDutyOrgan = new ArrayList<SysDutyOrgan>();
			for(String organId : arrOrganId){
				SysDutyOrgan sysDutyOrgan = new SysDutyOrgan();
				sysDutyOrgan.setId(BaseUuidUtil.getUUID());
				sysDutyOrgan.setDutyId(dutyId);
				sysDutyOrgan.setOrganId(organId);
				listSysDutyOrgan.add(sysDutyOrgan);
			}
			sysDutyOrganDao.insertBatch(listSysDutyOrgan);
		}
	}
	
    /**
     * DESC: 根据角色ID，查询角色的资源(返回所有资源，通过ownerFlag进行标识)
     * @auther 胡义振
     * @date 2020年1月14日
     * @param dutyId
     * @return
     * @throws Exception 
     * @return
     */
    public List<SysOrgan> doGetAllOrganListByDutyId(String dutyId) throws Exception{
		Map<String,String> map = new HashMap<String,String>();
		map.put("dutyId", dutyId);
		return sysDutyOrganDao.selectAllOrganListByDutyId(map);
	}

	/**
	 * DESC: 根据职务而ID，查询职务管辖机构(返回所有资源，通过ownerFlag进行标识)
	 * @auther 胡义振
	 * @date 2020年1月14日
	 * @param dutyId
	 * @return
	 * @throws Exception 
	 * @return
	 */
    public List<JSONObject> doGetAllOrganJsonByDutyId(String dutyId) throws Exception{
		List<SysOrgan> listSysOrgan = doGetAllOrganListByDutyId(dutyId);
		return doExchangeSysOrganListToJson(listSysOrgan);
	}

    /**
     * Desc: 机构列表转为JSONObject 
     * @param listSysFunctionList
     * @return
     * @throws Exception
     * Date: 2020年1月14日
     * Author: Administrator
     */
    @Override
    public List<JSONObject> doExchangeSysOrganListToJson(List<SysOrgan> listSysOrgan) throws Exception {
		List<JSONObject> listParentSysFunctionJSONObject = new ArrayList<>();
		List<SysOrgan> parentSysOrganList = null;
		
		if(listSysOrgan!=null) {
			// 获取根节点
			parentSysOrganList = listSysOrgan.stream().filter( t -> t.getParentId().equals("0")).collect(Collectors.toList());
			// 添加每个根节点所有节点
			parentSysOrganList.stream().forEach(t -> {
				try {
					JSONObject treeObject = new JSONObject(true);
					treeObject.put("id", t.getId());
					treeObject.put("parentId", t.getParentId());
					treeObject.put("organCode", t.getOrganCode());
					treeObject.put("organName", t.getOrganName());
					treeObject.put("shortName", t.getShortName());
					treeObject.put("organType", t.getOrganType());
					treeObject.put("address", t.getAddress());
					treeObject.put("master", t.getMaster());
					treeObject.put("linkMan", t.getLinkMan());
					treeObject.put("linkpHone", t.getLinkPhone());
					treeObject.put("postCode", t.getPostCode());
					treeObject.put("status", t.getStatus());
					treeObject.put("remark", t.getRemark());
					treeObject.put("createUserId", t.getCreateUserId());
					treeObject.put("createUserName", t.getCreateUserName());
					treeObject.put("createTime", t.getCreateTime());
					treeObject.put("updateTime", t.getUpdateTime());
					treeObject.put("displayOrder", t.getDisplayOrder());
					treeObject.put("dependentAccount", t.getDependentAccount());
					treeObject.put("superName", t.getSuperName());
					treeObject.put("ownerFlag", t.getOwnerFlag());
					// 子节点（递归所有子节点）
					treeObject.put("children", getChildrenMenuJSONObject(listSysOrgan,t.getId()));
					listParentSysFunctionJSONObject.add(treeObject);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			});
		}
		return listParentSysFunctionJSONObject;
    }
    
    /**
     * Desc: 递归获取子节点
     * @param listSysFunctionList
     * @param parentId
     * @return
     * @throws Exception
     * Date: 2020年1月14日
     * Author: Administrator
     */
	private List<JSONObject> getChildrenMenuJSONObject(List<SysOrgan> listSysOrgan,String parentId) throws Exception  {
		List<JSONObject> listChildrenSysFunctionJSONObject = new ArrayList<>();
		List<SysOrgan> parentSysOrganList  = null;
		if(listSysOrgan!=null) {
			// 子节点列表
			parentSysOrganList = listSysOrgan.stream().filter( t -> t.getParentId().equals(parentId)).collect(Collectors.toList());
			// 遍历子节点
			if(parentSysOrganList!=null) {
				parentSysOrganList.stream().forEach(t -> {
					try {
						JSONObject treeObject = new JSONObject(true);
						treeObject.put("id", t.getId());
						treeObject.put("parentId", t.getParentId());
						treeObject.put("organCode", t.getOrganCode());
						treeObject.put("organName", t.getOrganName());
						treeObject.put("shortName", t.getShortName());
						treeObject.put("organType", t.getOrganType());
						treeObject.put("address", t.getAddress());
						treeObject.put("master", t.getMaster());
						treeObject.put("linkMan", t.getLinkMan());
						treeObject.put("linkpHone", t.getLinkPhone());
						treeObject.put("postCode", t.getPostCode());
						treeObject.put("status", t.getStatus());
						treeObject.put("remark", t.getRemark());
						treeObject.put("createUserId", t.getCreateUserId());
						treeObject.put("createUserName", t.getCreateUserName());
						treeObject.put("createTime", t.getCreateTime());
						treeObject.put("updateTime", t.getUpdateTime());
						treeObject.put("displayOrder", t.getDisplayOrder());
						treeObject.put("dependentAccount", t.getDependentAccount());
						treeObject.put("superName", t.getSuperName());
						treeObject.put("ownerFlag", t.getOwnerFlag());
						// 子节点（递归所有子节点）
						treeObject.put("children", getChildrenMenuJSONObject(listSysOrgan,t.getId()));
						listChildrenSysFunctionJSONObject.add(treeObject);
					} catch (Exception e) {
						e.printStackTrace();
					}
				});
			}
		}
		return listChildrenSysFunctionJSONObject;
	}

}
